/*
 * C1.3.6.c
 *
 *  Created on: 2013/01/10
 *      Author: hengzhong
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"

static const PSZ RuleID = "C1.3.6";

//TODO
static const PSZ MESSAGE_ENG = "";
static const PSZ MESSAGE_JPN = "予約済み'%s'識別子";
static const PSZ MESSAGE_CHS = "";
static const char Keyword[][256]={"defined","_LINE_","_FILE_","_DATE_","_TIME_","_STDC_","errno","assert"};

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(AST index, AST tuIdx) {
	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;
	
	AST codeScp = CDirectiveInfo.getCodeScp(index);
    PSZ CheckWord,opName;
    int Lenth= 0,i=0;

    Lenth=sizeof(Keyword)/sizeof(Keyword[0]);

	//TODO
    // Preseted Macro and Option in DB, so check by Scp
        if (codeScp==0){
        	return EXIT_SUCCESS;
        }
    //result = RuleCheck();
	//defined check
	if (CDirectiveInfo.isDefine(index)){
		CheckWord=CDirectiveInfo.define_getMacroName(index);
		for ( i=0;i<=Lenth;i++) {
			if (!strcmp(CheckWord,Keyword[i])){
				result = RULE_RESULT_NG;
				opName= CheckWord;
				break;
			}
		}
	}
	if (CDirectiveInfo.isUndef(index)){
		CheckWord= CDirectiveInfo.undef_getMacroName(index);
		for ( i=0;i<=Lenth;i++) {
			if (strcmp(CheckWord,Keyword[i])){
				result = RULE_RESULT_NG;
				opName= CheckWord;
				break;
			}
		}
	}
	
	if(result == RULE_RESULT_OK){
		return EXIT_SUCCESS;
	}
	
	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == index) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		//TODO
		//codeScp = ??;
		newNode->indexInfo = index;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		//TODO
		sprintf(newNode->msgBuf_eng, MESSAGE_ENG,opName);
		sprintf(newNode->msgBuf_chs, MESSAGE_CHS,opName);
		sprintf(newNode->msgBuf_jpn, MESSAGE_JPN,opName);

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID C1_3_6_register( VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cpp = RuleMain;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}
